<?php
namespace backend\services ;
use backend\models\AdminUser;
use yii\db\Query ;
use yii\data\Pagination ;
class MemberService {
    
    /**
     * 查找用户列表
     * @param int $limit
     */
    public static function findMemberList($limit,$type_id){
        $query = new Query() ;
        $pages = null ;
        $select = "*" ;
        $query->select($select)
              ->from("{{%AdminUser}}")
              ->orderBy("user_id asc") ;
        if(isset($limit)){
            $pages = new Pagination(['totalCount' =>$query->count(), 'defaultPageSize' => $limit]);
            $query->offset($pages->offset)
                ->limit($limit) ;
        }        
        
        $member_list = $query->all() ;
        
        return ["list"=>$member_list,"pages"=>$pages] ;
        
    }

    /**
     * 删除用户
     * @param $user_id
     * @return array
     */
    public static function delUserById($user_id){
        $user = AdminUser::findOne(["user_id"=>$user_id]) ;
        if(empty($user)){
            return ["status"=>false,"msg"=>"用户不存在"];
        }
        if($user['username']=="admin"){
            return ["status"=>false,"msg"=>"不允许删除超级管理员"];
        }
        $del_rs = $user->delete() ;
        if($del_rs==true){
            return ["status"=>true,"msg"=>"删除成功"];
        }else{
            return ["status"=>false,"msg"=>"删除失败"];
        }
    }

}
